<?php
    $_helper = $this->helper('catalog/output');
    $store = Mage::app()->getStore();
    $code  = $store->getCode();
?>
<?php if($this->getItems()->getSize()): ?>
<div class="block block-related">
    <div class="block-title">
        <span><?php echo $this->__('Related Products') ?>:</span>
    </div>
    <div class="block-content">
        <?php
            if(Mage::getStoreConfig("trego_settings/product_view/related_checkbox", $code)){
        ?>
        <p class="block-subtitle"><?php echo $this->__('Check items to add to the cart or') ?>&nbsp;<a href="#" onclick="selectAllRelated(this); return false;"><?php echo $this->__('select all') ?></a></p>
        <?php
            }
        ?>
        <ul class="products-grid bxslider" id="block_related">
        <?php foreach($this->getItems() as $_product): ?>
            <li class="item">
                <div class="product-image-area">
                    <?php if(!$_product->isComposite() && $_product->isSaleable()): ?>
                        <?php if (!$_product->getRequiredOptions() && Mage::getStoreConfig("trego_settings/product_view/related_checkbox", $code)): ?>
                            <input type="checkbox" class="checkbox related-checkbox" id="related-checkbox<?php echo $_product->getId() ?>" name="related_products[]" value="<?php echo $_product->getId() ?>" />
                            <div class="related-checkbox-alias" chkid="related-checkbox<?php echo $_product->getId() ?>"></div>
                        <?php endif; ?>
                    <?php endif; ?>
                    <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image">
                        <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(350); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>"/>
                    </a>
                    <?php 
                        $top_position = 10;
                        // Get the Special Price
                        $specialprice = Mage::getModel('catalog/product')->load($_product->getId())->getSpecialPrice(); 
                        // Get the Special Price FROM date
                        $specialPriceFromDate = Mage::getModel('catalog/product')->load($_product->getId())->getSpecialFromDate();
                        // Get the Special Price TO date
                        $specialPriceToDate = Mage::getModel('catalog/product')->load($_product->getId())->getSpecialToDate();
                        // Get Current date
                        $today =  time();
                     
                        if ($specialprice){
                            if($today >= strtotime( $specialPriceFromDate) && $today <= strtotime($specialPriceToDate) || $today >= strtotime( $specialPriceFromDate) && is_null($specialPriceToDate)){
                                if(Mage::getStoreConfig("trego_settings/product_label/sale", $code)){
                    ?>
                                <div class="sale-product-icon" style="top: <?php echo $top_position; ?>px"><?php echo $this->__('Sale');?></div>
                    <?php       
                                $top_position += 20;
                                }
                            }
                        }
                    ?>
                    <?php
                        $now = date("Y-m-d");
                        $newsFrom= substr($_product->getData('news_from_date'),0,10);
                        $newsTo=  substr($_product->getData('news_to_date'),0,10);
                        if ($newsTo != '' || $newsFrom != ''){
                            if (($newsTo != '' && $newsFrom != '' && $now>=$newsFrom && $now<=$newsTo) || ($newsTo == '' && $now >=$newsFrom) || ($newsFrom == '' && $now<=$newsTo))
                            {
                                if(Mage::getStoreConfig("trego_settings/product_label/new", $code)){
                        ?> 
                                <div class="new-product-icon" style="top: <?php echo $top_position; ?>px"><?php echo $this->__('New');?></div>
                        <?php 
                                $top_position += 20;
                                }
                            }
                        }
                        if(Mage::getStoreConfig("trego_settings/category_grid/ratings", $code)){
                            $this->_reviewsHelperBlock = $this->getLayout()->createBlock('review/helper');
                            echo $this->_reviewsHelperBlock->getSummaryHtml($_product, 'short', false);
                        }
                    ?>
                    <div class="clearer"></div>
                </div>
                <?php echo $this->getPriceHtml($_product, true) ?>
                <h2 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a></h2>
            </li>
        <?php endforeach ?>
        </ul>
        <script type="text/javascript">decorateList('block-related', 'none-recursive')</script>
    </div>
    <script type="text/javascript">
    //<![CDATA[
    $$('.related-checkbox').each(function(elem){
        Event.observe(elem, 'click', addRelatedToProduct)
    });

    var relatedProductsCheckFlag = false;
    function selectAllRelated(txt){
        if (relatedProductsCheckFlag == false) {
            $$('.related-checkbox').each(function(elem){
                elem.checked = true;
            });
            relatedProductsCheckFlag = true;
            txt.innerHTML="<?php echo $this->__('unselect all') ?>";
        } else {
            $$('.related-checkbox').each(function(elem){
                elem.checked = false;
            });
            relatedProductsCheckFlag = false;
            txt.innerHTML="<?php echo $this->__('select all') ?>";
        }
        addRelatedToProduct();
    }

    function addRelatedToProduct(){
        var checkboxes = $$('.related-checkbox');
        var values = [];
        for(var i=0;i<checkboxes.length;i++){
            if(checkboxes[i].checked) values.push(checkboxes[i].value);
        }
        if($('related-products-field')){
            $('related-products-field').value = values.join(',');
        }
    }
    //]]>
    </script>
</div>
<script type="text/javascript">
    jQuery(document).ready(function(){
        var _width = jQuery(window).width();
        var _slidewidth = 200;
        var _slidemargin = 20;
        var _maxslides = 4;
        if(_width > 1024 && _width <=1099){
            _slidewidth = 181;
        }
        if(_width >= 768 && _width <=1024){
            _slidewidth = 170;
            _slidemargin = 16;
        }
        if(_width < 768 && _width >= 700){
            _maxslides = 3;
        }
        if(_width < 700 && _width >= 520){
            _maxslides = 2;
        }
        if(_width < 520){
            _maxslides = 1;
        }
        _width = jQuery('.main').width();
        _slidewidth = (_width-_slidemargin*(_maxslides-1))/_maxslides;
        var related_slider = jQuery('#block_related').bxSlider({
            minSlides: 1,
            maxSlides: _maxslides,
            pager: false,
            slideWidth: _slidewidth,
            slideMargin: _slidemargin,
            responsive: true
        });
        var st;
        jQuery(window).resize(function(e){
            e.preventDefault();
            var _width = jQuery(document).width();
            if(st) clearTimeout(st);
            st = setTimeout(function(){
                var _width = jQuery(window).width();
                var _slidewidth = 200;
                var _slidemargin = 20;
                var _maxslides = 4;
                 if(_width > 1024 && _width <=1099){
                    _slidewidth = 181;
                }
                if(_width >= 768 && _width <=1024){
                    _slidewidth = 170;
                    _slidemargin = 16;
                }
                if(_width < 768 && _width >= 700){
                    _maxslides = 3;
                }
                if(_width < 700 && _width >= 520){
                    _maxslides = 2;
                }
                if(_width < 520){
                    _maxslides = 1;
                }
                _width = jQuery('.main').width();
                _slidewidth = (_width-_slidemargin*(_maxslides-1))/_maxslides;
                related_slider.reloadSlider({
                    minSlides: 1,
                    maxSlides: _maxslides,
                    pager: false,
                    slideWidth: _slidewidth,
                    slideMargin: _slidemargin,
                    responsive: true
                });
            }, 500);
        });
        jQuery('.related-checkbox-alias').click(function(){
            if(jQuery(this).hasClass('checked')){
                jQuery(this).removeClass('checked');
            } else{
                jQuery(this).addClass('checked');
            }
            jQuery('#'+jQuery(this).attr('chkid')).click();
        });
    })
</script>
<?php endif ?>
